package com.layui.ad.controller;

import com.layui.ad.config.shiro.entiry.ShiroUser;
import com.layui.ad.entity.SysAdmin;
import com.layui.ad.service.SysAdminService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.utils.RandomValidateCodeUtil;
import com.layui.ad.utils.RedisUtil;
import com.layui.ad.vo.AdminPwdVo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/auth")
@Log4j2
public class AuthController {

    @Resource
    private SysAdminService sysAdminService;

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
//    @ResponseBody
    public String login() {
//        String username = "627770";
//        String password = "admin";
//        gmSysAdminService.login(username,password);
        return "login";
    }

    @RequestMapping("/checklogin")
    @ResponseBody
    public JsonResult checklogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String random = (String) session.getAttribute("checkCode");
        String code = request.getParameter("captcha");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        log.info("random:"+random);
        log.info("code:"+code);
        if(random.equals(code) == false){
            return JsonResult.fail("验证码错误");
        }
        boolean ret = sysAdminService.login(username,password);
        if(ret){
            redisUtil.set("code",code);
            return JsonResult.ok();
        }else{
            return JsonResult.fail("登录失败");
        }
    }


    @RequestMapping("/user")
    @ResponseBody
    public String user() {
        ShiroUser data = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println(data.getBrokerId());
        System.out.println(redisUtil.get("code"));
        return data.getBrokerId();
    }

    @RequestMapping("/logout")
    @ResponseBody
    public void logout(HttpServletResponse response) throws IOException {
        Subject subject = SecurityUtils.getSubject();//取出当前验证主体
        if (subject != null) {
            subject.logout();//不为空，执行一次logout的操作，将session全部清空
        }
        response.sendRedirect("/auth/login");
//        return "redirect:/auth/login";
    }

    /**
     * 生成验证码
     */
    @RequestMapping(value = "/verify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            log.error("获取验证码失败>>>> ", e);
        }
    }

    @ResponseBody
    @PostMapping("/updatePassword")
    public JsonResult updatePassword(@RequestBody AdminPwdVo entiry){
        return sysAdminService.updatePassword(entiry);
    }
}
